<template>
  <section>
    <el-card :span="24" class="toolbar" style="padding-bottom: 0px;">
      <el-form :inline="true" @submit.native.prevent>
        <el-form-item>
          <el-input v-model="filters.keyword" placeholder="请输入搜索内容"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="getList">查询</el-button>
        </el-form-item>
        <el-form-item v-if="buttonFuncs.handleAdd">
          <el-button type="primary" @click="handleAdd">新增</el-button>
        </el-form-item>
      </el-form>
    </el-card>

    <!--列表-->
    <el-table
      :data="groupList"
      v-loading="listLoading"
      style="width: 100%"
      @select="dialogCheck"
      @row-click="selectCurrentRow"
      ref="table"
      class="custom-tbl"
    >
      <el-table-column prop="Id" label="ID" width="100" sortable>
      </el-table-column>
      <el-table-column prop="GroupName" label="名称" width="" sortable>
      </el-table-column>
      <el-table-column prop="Index" label="排序" width="" sortable>
      </el-table-column>
      <el-table-column prop="Description" label="描述" width="400" sortable>
        <template #default="scope">
          <span v-html="scope.row.Description.substring(0, 100)"></span>
        </template>
      </el-table-column>
      <el-table-column
        prop="CreateTime"
        label="创建时间"
        width="250"
        sortable
      >
      </el-table-column>
      <el-table-column label="操作" fixed="right">
        <template #default="scope">
          <div v-for="item in operateButtons" v-bind:key="item.id">
            <el-button link type="primary" size="small" @click="callFunction(item, scope.row)">{{item.name}}</el-button>
          </div>
        </template>
      </el-table-column>
    </el-table>

    <!--工具条-->
    <el-col :span="24" class="toolbar">
      <el-pagination
        layout="prev, pager, next"
        @current-change="handleCurrentChange"
        :page-size="6"
        :total="total"
        style="float: right"
      >
      </el-pagination>
    </el-col>

    <!--编辑界面-->
    <el-dialog
      :title="dialogTitle"
      v-model="editFormVisible"
      :close-on-click-modal="false"
    >
      <el-form
        :model="editForm"
        label-width="80px"
        :rules="editFormRules"
        ref="editForm"
      >
        <el-input v-model="editForm.Id" auto-complete="off" v-show="false"></el-input>
        <el-form-item label="名称" prop="GroupName">
          <el-input v-model="editForm.GroupName" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="排序" prop="Index">
          <el-input v-model="editForm.Index" auto-complete="off" type="number"></el-input>
        </el-form-item>
        <el-form-item label="描述" prop="Description">
          <el-input v-model="editForm.Description" auto-complete="off"></el-input>
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button @click.native="editCancel">取消</el-button>
          <el-button
            type="primary"
            @click.native="editSubmit"
            :loading="editLoading"
            >提交</el-button
          >
        </div>
      </template>
    </el-dialog>
  </section>
</template>

<script>
import { getDeviceGroupListPage, removeDeviceGroup, editDeviceGroup} from "/@/api/device";
import { getButtonMap } from "/@/router/backEnd";
import { ElMessage} from 'element-plus'

export default {
  data() {
    return {
      buttonFuncs: [],
      operateButtons: [],
      currentRow: null,
      filters: {
        keyword: "",
      },
      groupList: [],
      total: 0,
      page: 1,
      listLoading: false,
      sels: [], //列表选中列
      addDialogFormVisible: false,
      editFormVisible: false, //编辑界面是否显示
      editLoading: false,
      editFormRules: {
        GroupName: [
          { required: true, message: "请输入名称", trigger: "blur" },
        ],
      },
      dialogTitle: '编辑',
      //编辑界面数据
      editForm: {
        Id: 0,
        Index: 0,
        GroupName: "",
        Description: "",
      },
    };
  },
  methods: {
    dialogCheck(selection, row) {
      this.currentRow = null;
      this.$refs.table.clearSelection();
      if (selection.length === 0) {
        return;
      }
      if (row) {
        this.selectCurrentRow(row);
      }
    },
    selectCurrentRow(val) {
      if (val) {
        this.currentRow = val;
        this.$refs.table.clearSelection();
        this.$refs.table.toggleRowSelection(val, true);
      }
    },
    callFunction(item, row) {
      this[item.Func].call(this, row);
    },
    handleCurrentChange(val) {
      this.page = val;
      this.getBlogs();
    },
    getList() {
      let para = {
        page: this.page,
        key: this.filters.keyword,
      };
      this.listLoading = true;

      //NProgress.start();
      getDeviceGroupListPage(para).then((res) => {
        this.total = res.data.dataCount;
        this.groupList = res.data.data;
        this.listLoading = false;
        //NProgress.done();
      });
    },
    //删除
    handleDel(row) {
      this.$confirm("确认删除该记录吗?", "提示", {
        type: "warning",
      })
        .then(() => {
          this.listLoading = true;
          //NProgress.start();
          let para = { id: row.Id };
          removeDeviceGroup(para).then((res) => {
            this.listLoading = false;
            //NProgress.done();
            if (res.success) {
              ElMessage({
                message: "删除成功",
                type: "success",
              });
            } else {
              ElMessage({
                message: res.msg,
                type: "error",
              });
            }

            this.getBlogs();
          });
        })
        .catch(() => {});
    },
    //显示编辑界面
    handleEdit(row) {
      if (!row) {
        ElMessage({
          message: "请选择要编辑的一行数据！",
          type: "error",
        });
        
        return;
      }
      this.editForm = row;
      this.dialogTitle = '编辑'
      this.editFormVisible = true;

    },
    //显示新增界面
    handleAdd() {
      this.editFormVisible = true;
      this.dialogTitle = '新增设备组'
      this.editForm = {Index:0};
    },
    editCancel() {
      this.editFormVisible = false;
      this.editLoading = false;
      this.editForm = {Index:0};
    },
    //编辑
    editSubmit: function () {
      this.$refs.editForm.validate((valid) => {
        if (valid) {
          this.$confirm("确认提交吗？", "提示", {}).then(() => {
            this.editLoading = true;
            //NProgress.start();
            let para = Object.assign({}, this.editForm);

            editDeviceGroup(para).then((res) => {

              if (res.success) {
                this.editLoading = false;
                //NProgress.done();
                ElMessage({
                  message: res.msg,
                  type: "success",
                });
                this.$refs["editForm"].resetFields();
                this.editFormVisible = false;
                this.getList();
              } else {
                ElMessage({
                  message: res.msg,
                  type: "error",
                });
              }
            });
          });
        }
      });
    },
  },
  mounted() {
    this.getList();

    let buttonMap = getButtonMap(this.$route.path);
    this.buttonFuncs = buttonMap['buttonFuncs']|| {};
    this.operateButtons = buttonMap['operation'] || [];
  },
};
</script>

<style scoped>
.custom-tbl :deep() .has-gutter .el-checkbox {
  display: none;
}
</style>
